package com.web.controller;

import com.web.entity.User;
import com.web.service.UserService;
import com.web.common.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("/findAll")
    public List<User> findAll() {
        return userService.findAll();
    }

    @RequestMapping("/get")
    public User get(Integer id) {
        return this.userService.findById(id);
    }
    @PostMapping("/login")
    public Result login(@RequestBody User user) {
        User loginUser = userService.login(user.getUsername(), user.getPassword());
        if (loginUser != null) {
            return Result.success("登录成功", loginUser);
        } else {
            return Result.fail("用户名或密码错误");
        }
    }
    // 新增注册接口
    @PostMapping("/register")
    public Result register(@RequestBody User user) {
        boolean success = userService.register(user);
        if (success) {
            return Result.success("注册成功");
        } else {
            return Result.fail("用户名已存在，注册失败");
        }
    }
}
